<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="isLoading">
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="form"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
              :model="userForm"
              :rules="rules"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userForm.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userForm.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" :lazy="true">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" :lazy="true">
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployeesInfo, updateEmployeesInfo } from '@/api/employees'
import UserInfo from '@/views/employees/components/UserInfo'
import JobInfo from '@/views/employees/components/JobInfo'

export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      id: this.$route.params.id, // 路由上边的id
      isLoading: false,
      userForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码长度最少6位', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.isLoading = true
    this.loadEmployeesInfo()
  },
  methods: {
    // 加载个人用户信息得到表格
    async loadEmployeesInfo() {
      this.userForm = await getEmployeesInfo(this.id)
      this.isLoading = false
    },
    // 点击更新
    async updateUserInfo() {
      try {
        await this.$refs.form.validate()
        await updateEmployeesInfo({ ...this.userForm, id: this.id })
        this.$message.success('更新基本信息成功')
      } catch (e) {
        return false
      }
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
